package com.zzyl;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.*;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@SpringBootTest
public class test {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Test
    public void test() {
        ValueOperations<String, String> op = redisTemplate.opsForValue();
        op.set("hhh","我好累呀");
        String s = op.get("hhh");
        System.out.println(s);
        op.set("王源","芙蓉王",1, TimeUnit.MINUTES);

        Boolean b = op.setIfAbsent("王扁", "利群");
        System.out.println(b);
        Boolean b1 = op.setIfAbsent("王源", "利群");
        System.out.println(b1
        );
    }

    @Test
    public void test1(){
        HashOperations<String, Object, Object> op = redisTemplate.opsForHash();
        op.put("守旧派","王源","芙蓉王");
        Object o = op.get("守旧派", "王源");
        System.out.println(o);
        Set<Object> set = op.keys("守旧派");
        System.out.println(set);
        List<Object> values = op.values("守旧派");
        System.out.println(values);
        op.delete("守旧派","王源");
    }
    @Test
    public void test2(){
        ListOperations<String, String> ops = redisTemplate.opsForList();
        ops.leftPushAll("二刺猿","原神","泵铁","鸣潮");
        // 在列表左边插入一个值[d,a,b,c]
        ops.leftPush("二刺猿","农药");
        // 获取列表中的数据
        ops.range("二刺猿", 0, -1);
        // 从左边弹出一个，并获取值，弹出后列表中删除
        String s = ops.leftPop("二刺猿");
        System.out.println(s);
        // 获取列表的长度
        Long size = ops.size("二刺猿");
        System.out.println(size);
    }
    @Test
    public void test3(){
        SetOperations<String, String> ops = redisTemplate.opsForSet();
        // 添加数据
        ops.add("射手","鲁班","后羿","马可");
        ops.add("打野","澜","猴子","马可");
        // 获取集合中的所有成员
        Set<String> members = ops.members("射手");
        System.out.println(members);
        // 获取集合大小
        int size = members.size();
        System.out.println(size);
        // 交集
        Set<String> intersect = ops.intersect("射手", "打野");
        System.out.println(intersect);
        // bing集
        Set<String> union = ops.union("射手", "打野");
        System.out.println(union);
        //cha集
        Set<String> difference = ops.difference("射手", "打野");
        System.out.println(difference);
    }
    @Test
    public void test4(){
        ZSetOperations<String, String> ops = redisTemplate.opsForZSet();
        ops.add("战士","花木兰",1d);
        ops.add("战士","吕布",2d);
        ops.add("战士","宫本",3d);
        // 获取集合中的所有成员
        Set<String> set = ops.range("战士", 0, -1);
        System.out.println(set);

        Set<ZSetOperations.TypedTuple<String>> set3 = ops.rangeWithScores("战士", 0, -1);
        System.out.println(set3);
        for (ZSetOperations.TypedTuple<String> stringTypedTuple : set3) {
            String value = stringTypedTuple.getValue();
            Double score = stringTypedTuple.getScore();
            System.out.println(value+score);
        }

        // 给a成员的分数增加10
        ops.incrementScore("战士","花木兰",10d);
        // 删除a、b两个成员
        ops.remove("战士","宫本");
        Set<String> set1 = ops.range("战士", 0, -1);
        System.out.println(set1);

    }
}
